<?php
/**
 * DMFramework
 * 
 * @license Copyright 2012 Dillen Meijboom under the Apache license version 2.0 (http://apache.org/licenses/LICENSE-2.0)
 * @author Dillen Meijboom <info@dillenm.nl>
 * @url http://dmframework.nl
 */

/**
 * @license Copyright 2012 Dillen Meijboom under the Apache license version 2.0 (http://apache.org/licenses/LICENSE-2.0)
 * @author Dillen Meijboom <info@dillenm.nl>
 * @package System.Library.Acl
 */
class Dmf_Acl_Role
{
    /**
     * Defines the name of this ACL group.
     *
     * @access private
     * @var string
     */
	private $_name;
    
    /**
     * Contains all exceptions
     * 
     * @var type 
     */
    private $_resources = array();
    
    /**
     * Constructor
     * 
     * @param string $name 
     */
    public function __construct( $name )
    {
        $this->_name = $name;
    }
    
    /**
     * Get Role name
     * 
     * @return string
     */
    public function getName()
    {
        return $this->_name;
    }
    
    /**
     * Add resource (if not exists) and attach actions to it
     * 
     * @param string $resource
     * @param array $actions 
     */
    public function addResource( $resource, $actions )
    {
        $resource_name = ( $resource instanceof Dmf_Acl_Resource ) ? $resource->getName() : $resource;
        
        if ( ! isset( $this->_resources[ $resource_name ] ) )
        {
            $this->_resources[ $resource_name ] = ( $resource instanceof Dmf_Acl_Resource ) ? $resource : new Dmf_Acl_Resource( $resource_name );
        }
        
        $this->_resources[ $resource_name ]->addActions( $actions );
    }
    
    /**
     * If resource exists
     * 
     * @param string $name
     * @return boolean
     */
    public function isResource( $name )
    {
        return isset( $this->_resources[ $name ] );
    }
    
    /**
     * If this Role has access to a certain resource and action
     * 
     * @param string $resource
     * @param string $action
     * @return boolean 
     */
    public function hasAccess( $resource, $action )
    {
        return $this->isResource( $resource ) ? $this->_resources[ $resource ]->isAction( $action ) : false;
    }
    
    /**
     * Set all resources
     * 
     * @param array $resources 
     */
    public function setResources( array $resources )
    {
        $this->_resources = $resources;
    }
    
    /**
     * Get all resources
     * 
     * @return array 
     */
    public function getResources()
    {
        return $this->_resources;
    }
}